#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "srf.h"
#include "aboutsrfreader.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    strFileBase = "";
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButtonRead_clicked()
{
    strFileBase = QFileDialog::getOpenFileName(
                this,
                "Open SRF file",
                "",
                tr("SRF (*.srf);; All files (*.*)"));

    if( strFileBase == "" )
    {
        ui->lineEditFileName->setText("No SRF file loaded");
        return;
    }

    ui->lineEditFileName->setText("Reading "+strFileBase);
    update();

    SRF mySRF;
    mySRF.Read(strFileBase.toStdString().c_str());

    QString str;
    str.sprintf("ID->\"%s\"\n", mySRF.garmin);
    ui->textEditFileInfo->setText(str);

    str.sprintf("3 ints %d, %d, %d\n", mySRF.hdrN1, mySRF.hdrN2, mySRF.hdrN3);
    ui->textEditFileInfo->append(str);

    for(int i=0; i<mySRF.nStringCount; i++)
    {
        str.sprintf("String %d \"%s\"", i+1, mySRF.strings[i].toStdString().c_str());
        ui->textEditFileInfo->append(str);
    }

    str.sprintf("\n3D view info size %d", mySRF.nSize3DInfo);
    ui->textEditFileInfo->append(str);
    str.sprintf("3D  mask height %d, width %d, length %d", mySRF.tdHeight, mySRF.tdWidth, mySRF.nLen3DMask);
    ui->textEditFileInfo->append(str);
    str.sprintf("3D image height %d, width %d, length %d", mySRF.tdHeight, mySRF.tdWidth, mySRF.nLen3DRGB);
    ui->textEditFileInfo->append(str);
    str.sprintf("3D view padding per line %d", mySRF.tdWidth - mySRF.tdHeight * 36);
    ui->textEditFileInfo->append(str);

    str.sprintf("\nTop view info size %d", mySRF.nSizeTopInfo);
    ui->textEditFileInfo->append(str);
    str.sprintf("Top  mask height %d, width %d, length %d", mySRF.topHeight, mySRF.topWidth, mySRF.nLenTopMask);
    ui->textEditFileInfo->append(str);
    str.sprintf("Top image height %d, width %d, length %d", mySRF.topHeight, mySRF.topWidth, mySRF.nLenTopRGB);
    ui->textEditFileInfo->append(str);
    str.sprintf("Top view padding per line %d", mySRF.topWidth - mySRF.topHeight * 36);
    ui->textEditFileInfo->append(str);

    update();

    strFileBase = strFileBase.left(strFileBase.length()-4);
    mySRF.Write(strFileBase.toStdString().c_str());

    ui->spinBoxSelect->setRange(0,35);
    ui->spinBoxSelect->setValue(0);
    SetImage();
    connect(ui->spinBoxSelect, SIGNAL(valueChanged(int)), this, SLOT(SetImage()));
    connect(ui->radioButton3D, SIGNAL( toggled( bool ) ), this, SLOT( SetImage() ) );
    connect(ui->radioButtonTop, SIGNAL( toggled( bool ) ), this, SLOT( SetImage() ) );
}

void MainWindow::SetImage()
{
    if( strFileBase == "" )
        return;

    int nIndex = ui->spinBoxSelect->value();
    char strFileImage[256];

    if( ui->radioButton3D->isChecked() )
    {
        sprintf(strFileImage, "%s_3D_%d0.png", strFileBase.toStdString().c_str(), nIndex);
    }
    else
    {
        sprintf(strFileImage, "%s_Top_%d0.png", strFileBase.toStdString().c_str(), nIndex);
    }

    ui->lineEditFileName->setText(strFileImage);

    QGraphicsScene *sceneImage = new QGraphicsScene();
    QPixmap pixmapImage(strFileImage);
    sceneImage->addPixmap(pixmapImage);
    ui->graphicsViewImage->setScene(sceneImage);
}

void MainWindow::on_pushButton_clicked()
{
    QString strFileBase = QFileDialog::getOpenFileName(
                this,
                "Select first image file",
                "",
                tr("PNG (*.png);; All files (*.*)"));

    if( strFileBase == "" )
    {
        ui->lineEditFileName->setText("No PNG file selected");
        return;
    }

    strFileBase = strFileBase.left(strFileBase.lastIndexOf("_3D"));

    SRF mySRF;

    if( !mySRF.CheckFiles(strFileBase.toStdString().c_str()) )
    {
        ui->textEditFileInfo->setText("Problem with input files?");
        ui->textEditFileInfo->append(mySRF.lastErr);
        return;
    }

    mySRF.Create(strFileBase.toStdString().c_str());
    ui->textEditFileInfo->setText("SRF file created ok");
}

void MainWindow::on_actionAbout_SRF_Reader_2_triggered()
{
    AboutSRFReader aboutDialog;
    aboutDialog.exec();
}
